package com.example.securitydemo.controller;

import com.aaa.entity.TabRoleMenu;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.securitydemo.service.ITabRoleMenuService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 于永利
 * @since 2023-07-14
 */
@RestController
@RequestMapping("/tab-role-menu")
public class TabRoleMenuController {

    @Resource
    private ITabRoleMenuService tabRoleMenuService;

    // 根据角色的id  获取对应的资源的id
    @GetMapping
    public Result getAllByRid(Integer rid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid",rid);
        List<TabRoleMenu> list = tabRoleMenuService.list(queryWrapper);
        return Result.success(list);
    }

    // 获取当前登录的用户的资源的mcode
    @GetMapping("menucode")
    public Result menucode(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        System.out.println(details);
        Map<String, Object> map = (Map<String, Object>) details.getDecodedDetails();
        System.out.println(map);

        return Result.success(map);
    }

}
